// prime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
#include "memory.h"
#define MAXV 1000
unsigned char gBuffer[MAXV+10];



void PrimeVerify(long n, int base)
{
    long dividend, remainder,quotient, cnt;
    int first_zero = 1;

    dividend = 1;
    cnt = 1;

    printf("\n%4d\t\t\'", n);
    for (;;)
    {
        quotient = dividend/n;
        if (0 == quotient)
        {
            if (0 == first_zero)
            {
                printf("%1d", quotient);
            }
        } 
        else
        {
            first_zero = 0;
            printf("%1d", quotient);
        }
        remainder = dividend - (n*quotient);
        if (0 == remainder)
        {
            break;
        }
        dividend = remainder*base + 1;
        cnt++;
        if (cnt>MAXV)
        {
            break;
        }
    }
    printf("\t%4d\t", cnt);
}

long getSqrt(long n)
{
    long i;
    for (i=1;(i*i)<n;i++);
    return (i-1);
}

void PrimeSeek()
{
    int vsqrt = getSqrt(MAXV);
    int i;

    memset(gBuffer, 1, (MAXV+9)*sizeof(char));
    for (i=1;i<=(MAXV/2);i++)
    {
        gBuffer[i*2] = 0;
    }
    
    for (i = 3; i<=vsqrt;i+=2)
    {
        if (0==gBuffer[i])
        {
            continue;
        }
        int jmax = MAXV / i;
        for (int j=i; j<=jmax; j+=2)
        {
            gBuffer[i*j]=0;
        }
    }
    for (int i=7;i<MAXV;i++)
    {
        if (1 == gBuffer[i])
        {
            PrimeVerify(i, 2);
            //printf("%d ", i);
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    PrimeSeek();
    //PrimeVerifyOct(11);
	return 0;
}

